<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="客户编号" prop="companyCode">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户简称" prop="companyName">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入客户简称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户全称" prop="fullName">
        <el-input
          v-model="queryParams.AMOUNT"
          placeholder="请输入客户全称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="付款方式" prop="paymentType">
        <el-select
          v-model="queryParams.paymentType"
          placeholder="请选择"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="(item, index) in paymentTypeOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--            type="primary"-->
    <!--            plain-->
    <!--            icon="plus"-->
    <!--            size="mini"-->
    <!--            @click="handleAdd"-->
    <!--            v-hasPermi="['yanjing:account:add']"-->
    <!--        >新增</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--            type="success"-->
    <!--            plain-->
    <!--            icon="edit"-->
    <!--            size="mini"-->
    <!--            :disabled="single"-->
    <!--            @click="handleUpdate"-->
    <!--            v-hasPermi="['yanjing:account:edit']"-->
    <!--        >修改</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--            type="danger"-->
    <!--            plain-->
    <!--            icon="delete"-->
    <!--            size="mini"-->
    <!--            :disabled="multiple"-->
    <!--            @click="handleDelete"-->
    <!--            v-hasPermi="['yanjing:account:remove']"-->
    <!--        >删除</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--            type="warning"-->
    <!--            plain-->
    <!--            icon="download"-->
    <!--            size="mini"-->
    <!--            @click="handleExport"-->
    <!--            v-hasPermi="['yanjing:account:export']"-->
    <!--        >导出</el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table
      v-loading="loading"
      :data="accountList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="客户编号"
        align="center"
        prop="companyCode"
        width="110"
      />
      <el-table-column
        label="客户简称"
        align="center"
        prop="companyName"
        width="150"
      />
      <el-table-column label="客户全称" align="center" prop="fullName" />
      <el-table-column
        label="付款方式"
        align="center"
        prop="paymentType"
        width="80"
      >
        <template #default="scope">
          <dict-tag :options="pay_type" :value="scope.row.paymentType" />
        </template>
      </el-table-column>
      <el-table-column
        label="可用余额"
        align="center"
        prop="amount"
        width="100"
      />
      <el-table-column
        label="充值金额"
        align="center"
        prop="rechargeAmount"
        width="100"
      />
      <el-table-column
        label="消费金额"
        align="center"
        prop="paymentAmount"
        width="100"
      />
      <el-table-column
        label="退款金额"
        align="center"
        prop="refundAmount"
        width="100"
      />
      <el-table-column
        label="冻结余额"
        align="center"
        prop="frozenAmount"
        width="100"
      />
      <el-table-column
        label="提现金额"
        align="center"
        prop="drawAmount"
        width="100"
      />
      <el-table-column
        label="操作"
        width="200"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleView(scope.row, scope.index)"
            >详细</el-button
          >
          <!--          <el-button link type="primary" icon="View" @click="handleUpdate(scope.row)" v-hasPermi="['system:post:edit']">查看</el-button>-->
          <el-button
            link
            type="primary"
            icon="Plus"
            @click="handleCharge(scope.row)"
            >充值</el-button
          >
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleCash(scope.row)"
            >提现</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->

    <el-dialog
      v-bind="$attrs"
      v-model="open"
      title="查看客户资金帐信息"
      append-to-body
      width="1350px"
    >
      <div class="styled-div">客户资金信息</div>
      <br />
      <br />
      <el-form ref="userRef" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户编号" prop="companyCode">
              <el-input
                v-model="form.companyCode"
                placeholder=""
                :maxlength="50"
                show-word-limit
                clearable
                prefix-icon="mobile"
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户简称" prop="companyName">
              <el-input
                v-model="form.companyName"
                placeholder=""
                :maxlength="50"
                show-word-limit
                clearable
                prefix-icon="mobile"
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户全称" prop="fullName">
              <el-input
                v-model="form.fullName"
                placeholder=""
                :maxlength="50"
                show-word-limit
                clearable
                prefix-icon="mobile"
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
            <el-form-item label="可用余额" prop="amount">
              <el-input
                v-model="form.amount"
                placeholder=""
                readonly
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="充值金额" prop="rechargeAmount">
              <el-input
                v-model="form.rechargeAmount"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="退款金额" prop="refundAmount">
              <el-input
                v-model="form.refundAmount"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="冻结金额" prop="frozenAmount">
              <el-input
                v-model="form.frozenAmount"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="提现金额" prop="drawAmount">
              <el-input
                v-model="form.drawAmount"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="styled-div">其他信息</div>
      <br />
      <br />
      <el-tabs
        v-model="activeName"
        type="border-card"
        @tab-click="handleTabClick"
      >
        <el-tab-pane label="充值记录" name="first">
          <el-table :data="tab1List">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
              label="序号"
              type="index"
              width="60"
            ></el-table-column>
            <el-table-column
              label="交易流水号"
              align="center"
              prop="paymentTradeNo"
            />
            <el-table-column label="银行渠道" align="center" prop="gateway" />
            <el-table-column
              label="银行流水号"
              align="center"
              prop="gatewayTradeNo"
            />
            <el-table-column
              label="交易金额"
              align="center"
              prop="amountYuan"
            />
            <el-table-column label="交易状态" align="center" prop="statusZh" />
            <el-table-column
              label="交易创建时间"
              align="center"
              prop="createTime"
            />
            <el-table-column
              label="交易完成时间"
              align="center"
              prop="callbackTime"
            />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="提现记录" name="second">
          <el-table :data="tab2List">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
              label="序号"
              type="index"
              width="60"
            ></el-table-column>
            <el-table-column
              label="交易流水号"
              align="center"
              prop="paymentTradeNo"
            />
            <el-table-column label="银行渠道" align="center" prop="gateway" />
            <el-table-column
              label="银行流水号"
              align="center"
              prop="gatewayTradeNo"
            />
            <el-table-column
              label="交易金额"
              align="center"
              prop="amountYuan"
            />
            <el-table-column label="交易状态" align="center" prop="statusZh" />
            <el-table-column
              label="交易创建时间"
              align="center"
              prop="createTime"
            />
            <el-table-column
              label="交易完成时间"
              align="center"
              prop="callbackTime"
            />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="消费记录" name="third">
          <el-table :data="tab3List">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
              label="序号"
              type="index"
              width="60"
            ></el-table-column>
            <el-table-column
              label="交易流水号"
              align="center"
              prop="paymentTradeNo"
            />
            <el-table-column label="银行渠道" align="center" prop="gateway" />
            <el-table-column
              label="银行流水号"
              align="center"
              prop="gatewayTradeNo"
            />
            <el-table-column
              label="交易金额"
              align="center"
              prop="amountYuan"
            />
            <el-table-column label="交易状态" align="center" prop="statusZh" />
            <el-table-column
              label="交易创建时间"
              align="center"
              prop="createTime"
            />
            <el-table-column
              label="交易完成时间"
              align="center"
              prop="callbackTime"
            />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="退款记录" name="fourth">
          <el-table :data="tab4List">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
              label="序号"
              type="index"
              width="60"
            ></el-table-column>
            <el-table-column
              label="交易流水号"
              align="center"
              prop="paymentTradeNo"
            />
            <el-table-column label="银行渠道" align="center" prop="gateway" />
            <el-table-column
              label="银行流水号"
              align="center"
              prop="gatewayTradeNo"
            />
            <el-table-column
              label="交易金额"
              align="center"
              prop="amountYuan"
            />
            <el-table-column label="交易状态" align="center" prop="statusZh" />
            <el-table-column
              label="交易创建时间"
              align="center"
              prop="createTime"
            />
            <el-table-column
              label="交易完成时间"
              align="center"
              prop="callbackTime"
            />
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <br />
      <br />
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      v-bind="$attrs"
      v-model="openCharge"
      title="客户充值"
      append-to-body
      width="600px"
    >
      <el-form ref="chargeRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="业务类型" prop="bizType">
              <el-select
                v-model="form.bizType"
                readonly
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in payment_biz_type"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易对象" prop="object">
              <el-input
                v-model="form.object"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="对象名称" prop="companyName">
              <el-input
                v-model="form.companyName"
                placeholder=""
                readonly
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="对象编号" prop="objectNo">
              <el-input
                v-model="form.objectNo"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="充值时间" prop="createTime">
              <el-select
                v-model="form.createTime"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in company_status"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="充值金额" prop="type">
              <el-input
                v-model="form.password"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="注意事项" prop="status"> </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="充值说明" prop="address">
              <el-input
                v-model="form.address"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" v-if="isDetail" @click="submitForm"
          >确定</el-button
        >
        <el-button @click="cancelCharge">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Post">
import {
  listAccount,
  getAccount,
  delAccount,
  addAccount,
  updateAccount,
} from "@/api/company/account.js";

import { queryListByParam } from "@/api/bank/payment.js";

const { proxy } = getCurrentInstance();

const { pay_type } = proxy.useDict("pay_type");
const { payment_biz_type } = proxy.useDict("payment_biz_type");
const accountList = ref([]);
const tab1List = ref([]);
const tab2List = ref([]);
const tab3List = ref([]);
const tab4List = ref([]);

const open = ref(false);
const openCharge = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    paymentType: null,
    companyCode: null,
    companyName: null,
    fullName: null,
  },

  queryFormParams: {
    bizType: "",
    objectId: null,
  },
  rules: {
    fullName: [
      { required: true, message: "客户全称不能为空", trigger: "blur" },
    ],
    companyName: [
      { required: true, message: "客户简称不能为空", trigger: "blur" },
    ],
    companyStatus: [
      { required: true, message: "客户状态不能为空", trigger: "blur" },
    ],
    paymentType: [
      { required: true, message: "支付方式不能为空", trigger: "blur" },
    ],
    paymentRatio: [
      { required: true, message: "支付比例不能为空", trigger: "blur" },
    ],
  },
  paymentTypeOptions: [
    {
      label: "实时付款",
      value: 0,
    },
    {
      label: "信用付款",
      value: 1,
    },
  ],
});

const { queryParams, queryFormParams, form, rules, paymentTypeOptions } =
  toRefs(data);
function getList() {
  loading.value = true;
  listAccount(queryParams.value).then((response) => {
    accountList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function queryListByParamTable1(companyId) {
  queryFormParams.value.bizType = "RECHARGE";
  queryFormParams.value.objectId = companyId;
  queryListByParam(queryFormParams.value).then((response) => {
    tab1List.value = response.rows;
  });
}

function queryListByParamTable2(companyId) {
  queryFormParams.value.bizType = "DRAW";
  queryFormParams.value.objectId = companyId;
  queryListByParam(queryFormParams.value).then((response) => {
    tab2List.value = response.rows;
  });
}

function queryListByParamTable3(companyId) {
  queryFormParams.value.bizType = "PAYMENT";
  queryFormParams.value.objectId = companyId;
  queryListByParam(queryFormParams.value).then((response) => {
    tab3List.value = response.rows;
  });
}

function queryListByParamTable4(companyId) {
  queryFormParams.value.bizType = "REFUNDMENT";
  queryFormParams.value.objectId = companyId;
  queryListByParam(queryFormParams.value).then((response) => {
    tab4List.value = response.rows;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    paymentId: undefined,
    companyId: undefined,
    bizType: undefined,
    object: undefined,
    objectId: 0,
    objectNo: undefined,
    companyName: undefined,
  };
  proxy.resetForm("queryForm");
}

function resetCharge() {
  form.value = {
    bizType: "RECHARGE",
    object: "CUSTOMER",
    companyId: undefined,
    objectId: 0,
    objectNo: undefined,
    companyName: undefined,
  };
  proxy.resetForm("queryForm");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */

function handleView(row) {
  queryListByParamTable1(row.companyId);
  queryListByParamTable2(row.companyId);
  queryListByParamTable3(row.companyId);
  queryListByParamTable4(row.companyId);
  open.value = true;
  title.value = "查看客户资金信息";
  form.value = row;
}
/** 充值 */
function handleCharge(row) {
  resetCharge();
  openCharge.value = true;
  form.value = row;
}

function cancelCharge() {
  openCharge.value = false;
  resetCharge();
}

/** 提现 */
function handleCash(row) {}

getList();
</script>
<style>
.styled-div {
  font-family: "黑体", sans-serif;
  font-weight: bold;
  margin-top: 5px;
  margin-bottom: 5px;
}
.zdy {
  margin-top: 1px;
  margin-bottom: 1px;
}
</style>